/* vim: set expandtab shiftwidth=4: -*- mode: c++; c-basic-offset: 4; tab-width: 8; indent-tabs-mode: nil -*-
EasyForms
An easy-to-use program for completing scanned documents.
Copyright (C) 2006-2007 Wesley Crossman
Email: wesley@crossmans.net

You can redistribute and/or modify this software under the terms of the GNU
General Public License as published by the Free Software Foundation;
either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this
program; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
Suite 330, Boston, MA 02111-1307 USA */

#include "FormsDocument.h"
#include "FormsDocumentScene.h"
#include "quazip.h"
#include "quazipfile.h"

FormsPage::FormsPage(const QString &fileName, const QString &title, QObject *parent) : QObject(parent) {
    view = NULL;
    this->fileName = fileName;
    this->title = title;
}

FormsPage::~FormsPage() {
    free();
}

FormsDocumentScene *FormsPage::getPage() {
    createView();
    return view;
}

void FormsPage::free() {
    delete view;
    view = NULL;
}

void FormsPage::setTitle(const QString &title) {
    this->title = title;
    emit titleChanged(title);
}

void FormsPage::createView() {
    view = new FormsDocumentScene(this, QPixmap(fileName));
}




FormsDocument::FormsDocument(QObject *parent) : QObject(parent) {
    index = 0;
}

FormsDocument::FormsDocument(const QString &fileName, QObject *parent)
            : QObject(parent) {
    error.clear();

    //* Open the zip file
    QuaZip zip(fileName);
    if (!zip.open(QuaZip::mdUnzip)) {
        error = tr("The file could not be opened. (Error 0)");
        return;
    }
    QuaZipFile inFile(&zip);

    //* Open the file describing the pages
    if (!zip.setCurrentFile("pages.xml")) {
        error = tr("The file is corrupt and could not be opened. (Error 1)");
        return;
    }
    if (!inFile.open(QIODevice::ReadOnly)) {
        error = tr("The file is corrupt and could not be opened. (Error 2)");
        return;
    }
    QTextStream stream(&inFile);
    QString pages = stream.readAll();
    inFile.close();

    /*
    if (!zip.setCurrentFile("page1.png")) {
        error = tr("Error Opening File"),
                tr("The file is corrupt and could not be opened. (Error 1)");
        return;
    }
    {
    QuaZipFile inFile(&zip);
    if (!inFile.open(QIODevice::ReadOnly)) {
        error = tr("The file is corrupt and could not be opened. (Error 2)");
        return;
    }
    QImageReader reader(&inFile, "PNG");
    QImage img = reader.read();
    QPixmap pix = QPixmap::fromImage(img);
    */
}

FormsDocument::~FormsDocument() {
}

void FormsDocument::saveAs(const QString &fileName) {
}

void FormsDocument::save() {
}

void FormsDocument::toStart() {
    if (!atStart()) {
        index = 0;
        emit selectedPageChanged(index, pages[index]->getTitle());
    }
}

void FormsDocument::toEnd() {
    if (!atEnd()) {
        index = pages.size()-1;
        emit selectedPageChanged(index, pages[index]->getTitle());
    }
}

void FormsDocument::previous() {
    if (!atStart()) {
        --index;
        emit selectedPageChanged(index, pages[index]->getTitle());
    }
}

void FormsDocument::next() {
    if (!atEnd()) {
        ++index;
        emit selectedPageChanged(index, pages[index]->getTitle());
    }
}

FormsDocumentScene *FormsDocument::getPage() {
    return pages[index]->getPage();
}

void FormsDocument::addPage(const QString &fileName, QString title, int newIndex) {
    FormsPage *page = new FormsPage(fileName, title, this);
    pages.insert((newIndex == -1) ? index : newIndex, page);
    bool movedIndex = newIndex != -1 && newIndex < index;
    if (movedIndex) {
        ++index;
    }
    if (pages.size() == 1 || movedIndex) {
        emit selectedPageChanged(index, pages[index]->getTitle());
    }
    emit countChanged(pages.size());
}

void FormsDocument::deletePage(int indexToDel) {
    error.clear();
    if (indexToDel == index) {
        error = QString("Trying to delete page %1 while it's selected").arg(indexToDel);
        return;
    }
    FormsPage *page = pages[indexToDel];
    pages.removeAt(indexToDel);
    delete page;
    if (indexToDel < index) {
        --index;
        emit selectedPageChanged(index, pages[index]->getTitle());
    }
    emit countChanged(pages.size());
}

